package com.lc.pda.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lc.pda.dto.AgvDTO;
import com.lc.pda.dto.DeliveryDTO;
import com.lc.pda.dto.GroupReceiptDTO;
import com.lc.pda.dto.MaterialDTO;
import com.lc.pda.entity.WmsMaterial;
import com.lc.pda.exception.ApplicationException;
import com.lc.pda.service.WmsMaterialService;
import com.lc.pda.service.WmsReceiptService;
import com.lc.pda.service.WmsShipmentService;
import com.lc.pda.service.WmsTaskService;
import com.lc.pda.util.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/pda")
@Api(tags = "pda管理")
@AllArgsConstructor
public class PdaController {

    private final WmsMaterialService materialService;

    private final WmsShipmentService shipmentService;

    private final WmsReceiptService receiptService;

    private final WmsTaskService taskService;

    @GetMapping("/materialInfoByName")
    @ApiOperation("模糊查询物料详情")
    public RestResult getMaterialByName(@RequestParam @ApiParam(value = "物料名称") String materialName) {
        if (StringUtils.isEmpty(materialName)) {
            throw ApplicationException.systemException("物料名称不能为空");
        }
        return RestResult.success(materialService.list(Wrappers.<WmsMaterial>lambdaQuery().like(WmsMaterial::getMaterialName, materialName)));
    }

    @GetMapping("/materialInfoByCode")
    @ApiOperation("根据物料编号查询详情")
    public RestResult getMaterialByCode(@RequestParam @ApiParam(value = "物料编号") String materialCode) {
        if (StringUtils.isEmpty(materialCode)) {
            throw ApplicationException.systemException("物料编号不能为空");
        }
        return RestResult.success(materialService.list(Wrappers.<WmsMaterial>lambdaQuery().eq(WmsMaterial::getMaterialCode, materialCode)));
    }

    @GetMapping("/getReceipt")
    @ApiOperation("获取入库单")
    public RestResult getReceipt(@RequestParam @ApiParam(value = "入库单号") String receiptCode) {
        return RestResult.success(receiptService.getReceipt(receiptCode));
    }

    @PostMapping("/groupReceipt")
    @ApiOperation("组盘收货")
    public RestResult groupReceipt(@RequestBody @ApiParam(value = "组盘DTO") GroupReceiptDTO groupReceiptDTO) {
        return receiptService.groupReceipt(groupReceiptDTO);
    }


    @PostMapping("/callMaterial")
    @ApiOperation("车间叫料")
    public RestResult callMaterial(@RequestBody @ApiParam(name = "车间叫料DTO") List<MaterialDTO> materialDTOList) {
        return shipmentService.callMaterial(materialDTOList);
    }

    @PostMapping("/delivery")
    @ApiOperation("出库/下架")
    public RestResult delivery(@RequestBody @ApiParam(name = "出库DTO") DeliveryDTO deliveryDTO) {
        return shipmentService.delivery(deliveryDTO);
    }

    @PostMapping("/callAgv")
    @ApiOperation("呼叫AGV")
    public RestResult callAgv(@RequestBody @ApiParam(name = "呼叫AGVDTO") AgvDTO agvDTO) {
        return receiptService.callAgv(agvDTO);
    }

    @GetMapping("/getTask")
    @ApiOperation("获取未完成的出入库任务")
    public RestResult getTask(@RequestParam @ApiParam(value = "任务类型") String taskType, @RequestParam @ApiParam(value = "托盘号") String trayCode) {
        return taskService.getTask(taskType, trayCode);
    }


}
